import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import javax.swing.SwingUtilities;
import javax.swing.Timer;


public class RTSGameState implements GameState{
	int startX;
	int startY;
	int endX;
	int endY;

	List<Selectable> selectables;
    Collection<Selectable> selected;
	Timer t;
	public RTSGameState(Timer t){
		this.t = t;
		selectables = new ArrayList<Selectable>();
		selected = new HashSet<Selectable>();
		for(int i = 0; i < 20; ++i){
			Soldier s1 = new Soldier(selectables, i % 10 * 50, i /10 * 50);
			t.addActionListener(s1);
			selectables.add(s1);
		}
	}



	@Override
	public void mouseClicked(MouseEvent arg0) 
	{
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		if(SwingUtilities.isLeftMouseButton(arg0)){
			endX = startX = arg0.getX();
			endY = startY = arg0.getY();
			for(Selectable s: selected){
				s.becomeUnselected();
			}
			selected.clear();
		}
		if(SwingUtilities.isRightMouseButton(arg0)){
			for(Selectable s: selected){
				s.sendTo(arg0.getX(), arg0.getY());
			}
		}
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		if(SwingUtilities.isLeftMouseButton(arg0)){
			startX = startY = endX = endY = 0;
		}
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		if(SwingUtilities.isLeftMouseButton(arg0)){
			endX = arg0.getX();
			endY = arg0.getY();
			for(Selectable s: selected){
				s.becomeUnselected();
			}
			selected.clear();
			for(Selectable s: selectables){
				int upperX = Math.min(startX,  endX);
				int upperY = Math.min(startY,  endY);
				int lowerX = Math.max(startX,  endX);
				int lowerY = Math.max(startY,  endY);
				if(s.isIntersected(upperX, upperY, lowerX, lowerY)){
					s.becomeSelected();
					selected.add(s);
				}
			}
		}
		if(SwingUtilities.isRightMouseButton(arg0)){
			for(Selectable s: selected){
				s.sendTo(arg0.getX(), arg0.getY());
			}
		}
		
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent arg0) {

	}

	@Override
	public void draw(Graphics2D g) {
		// TODO Auto-generated method stub
		Stroke prevStroke = g.getStroke();
		g.setStroke(new BasicStroke(10.0f));
		Color prevColor =g.getColor(); 
		g.setColor(new Color(0,255,255,50));
		g.fillRect(startX,  startY,  endX - startX,  endY-startY);
		for(Selectable s: selectables){
			g.setColor(s.getColor());
			for(Shape sh: s.getShapes()){
				g.draw(sh);
			}
		}
		g.setColor(prevColor);
	}
}
